<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>发布消息</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="css/comments.css" media="all">
	<link rel="stylesheet" href="css/formSelects-v4.css" />
    <style>
        .layui-form-label{
            width: 110px;
        }
        .layui-input-block{
            margin-left: 140px;
        }
    </style>
</head>
<body>
    <div class="maincontainer">
        <blockquote class="layui-elem-quote">
            发布消息
            <button type="button" class="layui-btn layui-btn-sm" style="float: right;margin-top: -4px;" onclick="window.history.back();">返回</button>
        </blockquote>
        <form class="layui-form" action="" lay-filter="example">
            <div class="layui-form-item">
              <label class="layui-form-label">消息标题：</label>
              <div class="layui-input-inline">
                <input type="text" name="title" autocomplete="off" placeholder="请输入消息标题" class="layui-input">
              </div>
            </div>
			<div class="layui-form-item">
				<label class="layui-form-label">接收食堂</label>
				<div class="layui-input-block">
				<select name="student_id" xm-select="select1" id="mySelect" lay-filter="student" xm-select-skin="danger"></select>
				</div>
			</div>
            <div class="layui-form-item">
              <label class="layui-form-label">消息内容：</label>
              <div class="layui-input-block">
                <textarea name="content" placeholder="请输入消息内容" class="layui-textarea"></textarea>
              </div>
            </div>
            <div class="layui-form-item">
              <div class="layui-input-block">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="demo1">确认</button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="window.history.back()">取消</button>
              </div>
            </div>
        </form>
    </div>
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script>
	layui.config({
		base: 'plugins/layui/modules/' //此处路径请自行处理, 可以使用绝对路径
	}).extend({
		formSelects: 'formSelects-v4'
	}).use(['http', 'form','formSelects'], function() {
        var http = layui.http,
            form = layui.form,
			formSelects = layui.formSelects;
			
		http.get('food/manager/canteen/listAll',{},function(res){
		    if(res.code == 200){
				var data = res.data;
				var newData = [];
				for (var i = 0; i < data.length; i++) {
					var list ={
						"value": data[i].id,
						"name": data[i].canteenName
					}
					newData.push(list)
				}
				formSelects.data('select1', 'local', {
					arr:newData
				});
		    }else{
		        layer.msg(res.msg)
		    }
		})
        
        //监听提交
        form.on('submit(demo1)', function(data){
			if($.trim(data.field.title) == "") {
			    layer.msg('请输入消息标题')
			    return
			}
			var canteenIdSet = formSelects.value('select1', 'val');
			if(!canteenIdSet || canteenIdSet.length==0){
				layer.msg('请选择接收食堂')
				return
			}
            
            if($.trim(data.field.content) == "") {
                layer.msg('请输入消息内容')
                return
            }
            http.postJSON('food/manager/message/add',JSON.stringify({
                title: data.field.title,
                content: data.field.content,
                canteenIdSet: canteenIdSet
            }),function(res){
                if(res.code == 200){
                    layer.msg('操作成功')
                    setTimeout(function(){
                        window.history.back()
                    },500)
                }else{
                    layer.msg(res.msg)
                }
            })
        });
    })
</script>
</body>